<template>
  <div class="container">
    <h1 class="title">智能自习室预约系统</h1>
    <div class="login-box">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" style="width: 330px;">
        <el-form-item prop="name">
          <el-input prefix-icon="el-icon-user" v-model="ruleForm.name" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" type="password" v-model="ruleForm.password"
            placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item style="margin-bottom: 0px">
          <div class="ct-btn">
            <el-button type="primary" @click="doLogin">登录</el-button>
          </div>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginPage',

  data() {
    return {
      ruleForm: {
        password: '',
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入账户', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    doLogin() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.$http.post('/user/login', this.ruleForm).then((res) => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: 'Login Success!'
              });
              localStorage.setItem('name', this.ruleForm.name);
              localStorage.setItem('role', res.data.role);
              localStorage.setItem('token', res.data.token);
              localStorage.setItem('uid', res.data.uid);
              this.$router.push('/index')
            } else {
              this.$message({
                type: 'error',
                message: 'Incorrect username or password!'
              });
            }
          }).catch((err) => {
            console.log(err)
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  },
}
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 100%;
  width: 100%;
  background-color: #244566;
}

.title {
  font-size: 44px;
  color: white;
  padding: 10px 0 20px 0;
}

.login-box {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
  width: 400px;
  background-color: azure;
  border-radius: 5px;
}

.ct-btn {
  display: flex;
  justify-content: center;
}
</style>
